package com.cardbaobao.cardbabyclient.utils;

import android.app.Activity;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.ui.dialog.LoadingDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * 友盟分享工具类
 * Created by zad
 * 2016/10/20 10:04
 */

public class ShareUtil {
    /**
     * 分享
     *
     * @param activity
     * @param media    SHARE_MEDIA
     */
    public static void toShare(final Activity activity, final SHARE_MEDIA media) {
        final LoadingDialog dialog = new LoadingDialog(activity);
        dialog.setShowText("初始化...");
        UMImage image = new UMImage(activity, activity.getString(R.string.string_share_img_url));
        UMWeb webShare=new UMWeb(URLUtil.SHARE);
        webShare.setTitle(activity.getString(R.string.string_share_tip_title));
        webShare.setDescription(activity.getString(R.string.string_share_tip_content));
        webShare.setThumb(image);

        new ShareAction(activity).
                setPlatform(media).
                withMedia(webShare).
                setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                dialog.show();
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                dialog.dismiss();
                ToastUtil.showToast("分享成功");
                postBigData(media);
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                dialog.dismiss();
                ToastUtil.showToast("分享失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                dialog.dismiss();
                ToastUtil.showToast("分享取消");
            }
        }).share();
    }

    /**
     * 分享大数据统计埋点
     *
     * @param media
     */
    private static void postBigData(final SHARE_MEDIA media) {
        if (media == SHARE_MEDIA.WEIXIN) {
            //微信分享
            GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_SHARE, Google.ACTION_WECHAT);
        } else if (media == SHARE_MEDIA.WEIXIN_CIRCLE) {
            //微信朋友圈分享
            GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_SHARE, Google.ACTION_WECHAT_CIRCLE);
        } else if (media == SHARE_MEDIA.SINA) {
            //新浪微博分享
            GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_SHARE, Google.ACTION_SINA);
        } else if (media == SHARE_MEDIA.QQ) {
            //新浪微博分享
            GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_SHARE, Google.ACTION_QQ);

        }
    }

    /**
     * 分享列表
     */
    public static final SHARE_MEDIA[] SHARE_MEDIA_LIST = new SHARE_MEDIA[]{SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ};
}
